/**
 * Copyright 2016 JustWayward Team
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.htfyun.pdfview;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;

import com.htfyun.utils.DebugLog;

public class PDFViewPager extends ViewPager {

    protected Context context;
    
    private IMiddleClick middleClick;
    
    public interface IMiddleClick {
        void onMiddleClick();
    }

    public PDFViewPager(Context context, String pdfPath) {
        super(context);
        this.context = context;
        setOffscreenPageLimit(3);
        init(pdfPath);
    }

    protected void init(String pdfPath) {
        setClickable(true);
        initAdapter(context, pdfPath);
        
    }

    protected void initAdapter(Context context, String pdfPath) {
        setAdapter(new PDFPagerAdapter.Builder(context)
                .setPdfPath(pdfPath)
                .setOffScreenSize(getOffscreenPageLimit())
                .setOnPageClickListener(clickListener)
                .create());
        
//        addOnPageChangeListener(new OnPageChangeListener() {
//            @Override
//            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//                DebugLog.e("onPageScrolled position = " + position);
//            }
//
//            @Override
//            public void onPageSelected(int position) {
//                DebugLog.e("onPageSelected position = " + position);
//
//            }
//
//            @Override
//            public void onPageScrollStateChanged(int state) {
//                DebugLog.e("onPageScrollStateChanged state = " + state);
//
//            }
//        });
    }

    private OnPageClickListener clickListener = new OnPageClickListener() {

        @Override
        public void onPageTap(View view, float x, float y) {
            int item = getCurrentItem();
            int total = getAdapter().getCount();//getChildCount();

            DebugLog.e("hehe x = " + x + ". item = " + item + ", total = " + total);
            if (x < 0.33f && item > 0) {
                item -= 1;
                setCurrentItem(item);
            } else if (x >= 0.67f && item < total - 1) {
                item += 1;
                setCurrentItem(item);
            }

            if (x >= 0.33f && x <= 0.67f) {
                if (middleClick != null) {
                    middleClick.onMiddleClick();
                }
            }

            PDFPagerAdapter adapter = (PDFPagerAdapter)getAdapter();

            Bitmap bmp = adapter.bitmapContainer.getRenderBitmap(item);
            DebugLog.e("bmp.size = " + bmp.getWidth() + ", h = " + bmp.getHeight());

        }
    };

    public void setMiddleClick(IMiddleClick middleClick) {
        this.middleClick = middleClick;
    }

    @Override
    public void setCurrentItem(int item, boolean smoothScroll) {
        // TODO Auto-generated method stub
        super.setCurrentItem(item, smoothScroll);
    }

    @Override
    public void setCurrentItem(int item) {
        // TODO Auto-generated method stub
        super.setCurrentItem(item, false);//动画停止
        
    }

    public void close() {
        ((PDFPagerAdapter)getAdapter()).close();
    }

    @Override
    public boolean onTouchEvent(MotionEvent arg0) {

        
        /* return false;//super.onTouchEvent(arg0); */
//        if (noScroll)
            return false;
        
//        else
//            return super.onTouchEvent(arg0);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent arg0) {
//        if (noScroll)
            return false;
//        else
//            return super.onInterceptTouchEvent(arg0);
    }

}
